package yl.hs.bmipfull.service.material.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.material.*;
import yl.hs.bmipfull.pojo.material.PurchaseContractProductEntity;
import yl.hs.bmipfull.pojo.material.PurchaseReceiptEntity;
import yl.hs.bmipfull.pojo.production.BrickShipOrderModel;
import yl.hs.bmipfull.service.material.IPurchaseContractPriceAdjustmentService;
import yl.hs.bmipfull.service.material.IPurchaseContractProductService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;

import java.math.BigDecimal;
import java.util.*;

/**
 * 作用:  采购合同产品明细信息服务层接口实现<br/>
 * 创建时间: 2021年06月03日 15时38分01秒 <br/>
 *
 * @author 张凯
 */
@Service
public class PurchaseContractProductServiceImpl extends PurchaseBaseService implements IPurchaseContractProductService {

    @Autowired
    IPurchaseContractProductDao dao;
    @Autowired
    IRawMaterialInfoDao rawMaterialInfoDao;
    @Autowired
    IPurchaseContractPriceAdjustmentService adjustmentService;
    @Autowired
    IPurchaseContractPriceAdjustmentDao priceAdjustmentDao;
    @Autowired
    IPurchaseReceiptDao receiptDao;
    @Autowired
    IRawMaterialCateDao cateDao;
    @Override
    public List<PurchaseContractProductEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(PurchaseContractProductEntity.class);
        List<PurchaseContractProductEntity> contractProducts = dao.queryList(pageQuery.getQuery());
        contractProducts.forEach(t -> {
            float productPrice = adjustmentService.getProductPrice(t.getContractSn(),t.getRmSN(), new Date());
            if (productPrice != 0.00f) {
                t.setPrice(productPrice);
            }
        });
        return contractProducts ;
    }

    @Override
    public List<PurchaseContractProductEntity> queryListby(PageQuery pageQuery) {
        pageQuery.checkSortSides(PurchaseContractProductEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }


    @Override
    public PurchaseContractProductEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer savePurchaseContractProduct(List<PurchaseContractProductEntity> entities, String contractSn) {
        Integer result = 0;
        //删除合同产品信息
        if (dao.deleteByContractSn(contractSn) > 0) {
            //writeLog(ActionType.DELETE, contractSn, "删除采购合同产品明细信息信息", contractSn, PurchaseContractProductEntity.class);
        }
        for (PurchaseContractProductEntity entity : entities) {
            entity.setSn(queryNewSN(PurchaseContractProductEntity.class));
            result += dao.insertNew(entity);
            //writeLog(ActionType.CREATE, contractSn, "插入采购合同产品明细信息信息", Tl.toJson(entity), PurchaseContractProductEntity.class);
        }

        return result;
    }

    @Override
    public List<Map<String,Object>> queryListByContractSn(String contractSn) {
        var list=dao.queryListByContractSn(contractSn);
        List<Map<String,Object>> result=new ArrayList<>();
        for (var entity:list ) {
            var map=entity.toMap();
            var raw=rawMaterialInfoDao.findBySN(entity.getRmSN());
            if(raw!=null){
                map.put("rmCateName",raw.getRmCateName());
            }

            result.add(map);
        }
        return result;
    }

    @Override
    @Transactional
    public Integer delete(String[] sn) {

        return 0;
    }

    @Override
    public PurchaseContractProductEntity findByContract(String contractSn, String rmSN) {
        var entity = dao.findByContract(contractSn, rmSN);
        var money = priceAdjustmentDao.getProductPrice(contractSn,rmSN,new Date());
        if(money!=null){
            entity.setPrice(money);
        }
        return entity;
    }

    @Override
    public Integer updateStateBySn(String sn, int state) {
        return dao.updateStateBySn(sn,state);
    }

    @Override
    public List<PurchaseContractProductEntity> quantityByProductSN(String rmSN) {
        return dao.quantityByProductSN(rmSN);
    }

    @Override
    public List<Map<String,Object>> quantityGroupByRmSn() {
        var rm =  dao.quantityGroupByRmSn().stream().sorted(Comparator.comparing(PurchaseContractProductEntity::getQuantity).reversed()).toList();
        List<Map<String,Object>> data = new ArrayList<>();
        for (PurchaseContractProductEntity item : rm) {
            var map = item.toMap();
            var list = receiptDao.quantityByRmSn(item.getRmSN()).stream().map(PurchaseReceiptEntity::getNetWeight).reduce(BigDecimal::add);
            map.put("margin",item.getQuantity().subtract(list.isPresent()?list.get(): BigDecimal.valueOf(0)));
            map.put("RmCateName",cateDao.findBySN(item.getRmCate()).getCateName());
            if(item.getQuantity().subtract(list.isPresent()?list.get(): BigDecimal.valueOf(0)).compareTo(BigDecimal.valueOf(0))!=0){
                data.add(map);
            }
        }
        return data;
    }
}